//
//  CCRTWebViewVC.m
//  iSeal5200
//
//  Created by He Yuan on 12-12-14.
//  Copyright (c) 2012年 http://www.heyuan110.com. All rights reserved.
//

#import "WebViewVC.h"

@interface WebViewVC ()<UIWebViewDelegate>
{
    NSString *url;
    NSString *titleString;
}
@property(nonatomic,retain) NSString *url;
@end

@implementation WebViewVC
@synthesize WebView;
@synthesize opaqueview;
@synthesize activityIndicator;
@synthesize url = _url;

- (void)viewDidUnload
{
    self.WebView = nil;
    self.opaqueview = nil;
    self.activityIndicator = nil;
    [super viewDidUnload];
}

- (void)dealloc
{

}

- (id)initWithUrl:(NSString *)weburl title:(NSString *)title
{
    self = [super init];
    if (self)
    {
        _url = weburl;
        
        titleString = title;
    }
    return self;
}

-(void)loadView
{
    [super loadView];
    
    WebView = [[UIWebView alloc]initWithFrame:self.view.bounds];
    WebView.delegate = self;
    WebView.scalesPageToFit = YES;
    WebView.userInteractionEnabled = YES;
    [WebView  loadRequest:[ NSURLRequest requestWithURL:[NSURL URLWithString:_url]]];
    [self.view addSubview:WebView];
    
    opaqueview = [[UIView alloc]initWithFrame:WebView.frame];
    [opaqueview setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.4]];
    [self.view addSubview:opaqueview];
    
    activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(150, 210, 20, 20)];
    [activityIndicator startAnimating];
    [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [opaqueview addSubview:activityIndicator];
    
    UILabel *showLbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 170,self.view.bounds.size.width, 30)];
    [showLbl setFont:[UIFont systemFontOfSize:16.0]];
    [showLbl setBackgroundColor:[UIColor clearColor]];
    [showLbl setTextAlignment:UITextAlignmentCenter];
    [showLbl setText:@"正在努力加载"];
    [opaqueview addSubview:showLbl];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (titleString)
    {
        [self setTitle:titleString showBackButton:YES];
    }
    else
    {
        [self setTitle:HYLocalizedString(@"iSeal5200") showBackButton:YES];
    }
    
    UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 44.0f)];
    [rightView setBackgroundColor:[UIColor clearColor]];
    
    UIImage *img = [UIImage imageNamed:@"webview_back.png"];
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.userInteractionEnabled = YES;
    backButton.showsTouchWhenHighlighted = YES;
	backButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    [backButton addTarget:self action:@selector(webGoBack:) forControlEvents:UIControlEventTouchDown];
	backButton.frame = CGRectMake(10, (rightView.frame.size.height-img.size.height)/2,img.size.width,img.size.height);
	[backButton setBackgroundImage:img forState:UIControlStateNormal];
	[backButton setBackgroundImage:img forState:UIControlStateHighlighted];
    [rightView addSubview:backButton];
    
    UIImage *forwardImg = [UIImage imageNamed:@"webview_forward.png"];
    UIButton * forwardButton = [UIButton buttonWithType:UIButtonTypeCustom];
    forwardButton.userInteractionEnabled = YES;
    forwardButton.showsTouchWhenHighlighted = YES;
    [forwardButton addTarget:self action:@selector(webGoFroward:) forControlEvents:UIControlEventTouchDown];
	forwardButton.frame = CGRectMake(backButton.frame.origin.x+backButton.frame.size.width+30.0f, (rightView.frame.size.height-forwardImg.size.height)/2,forwardImg.size.width,forwardImg.size.height);
	[forwardButton setBackgroundImage:forwardImg forState:UIControlStateNormal];
	[forwardButton setBackgroundImage:forwardImg forState:UIControlStateHighlighted];
    [rightView addSubview:forwardButton];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightView];
	self.navigationItem.rightBarButtonItem = rightItem;
    
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage
                                          sharedHTTPCookieStorage];
    [cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
}

-(void)webGoFroward:(id)sender
{
	[WebView goForward];
}

-(void)webGoBack:(id)sender
{
	[WebView goBack];
}

- (void )webViewDidFinishLoad:(UIWebView *)webView
{
	opaqueview.hidden  = YES ;
}

- (void )webViewDidStartLoad:(UIWebView *)webView
{
	opaqueview.hidden  = NO ;
}

- (void)webView:(UIWebView *)webView  didFailLoadWithError:(NSError *)error
{
	opaqueview.hidden  = YES ;
}

- (void)viewWillAppear:(BOOL)animated
{
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
